﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emEducationDetail : common.control.baseUserControl
    {
        private BindingSource myDataSource = null;
        public emEducationDetail()
        {
            InitializeComponent();
            SetMaxLength();
        }

        public void SetDataSource(System.Windows.Forms.BindingSource dataSrc)
        {
            this.myDataSource = dataSrc;
            data.baseDS.emEducationDataTable tbl = (data.baseDS.emEducationDataTable)dataSrc.DataSource; 
            this.schoolNameEd.DataBindings.Clear();
            this.schoolNameEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.schoolNameColumn.ColumnName, true));

            this.countryCb.DataBindings.Clear();
            this.countryCb.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", dataSrc, tbl.schoolCountryColumn.ColumnName, true));

            this.degreeCb.DataBindings.Clear();
            this.degreeCb.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", dataSrc, tbl.degreeColumn.ColumnName, true));

            this.gradYearEd.DataBindings.Clear();
            this.gradYearEd.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", dataSrc, tbl.gradYearColumn.ColumnName, true));

            this.studyFieldEd.DataBindings.Clear();
            this.studyFieldEd.DataBindings.Add(new System.Windows.Forms.Binding("myItemString", dataSrc, tbl.fieldOfStudyColumn.ColumnName, true));

            this.noteEd.DataBindings.Clear();
            this.noteEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.notesColumn.ColumnName, true));
        }
        private void SetMaxLength()
        {
            data.baseDS.emEducationDataTable tbl = new data.baseDS.emEducationDataTable();
            schoolNameEd.MaxLength = tbl.schoolNameColumn.MaxLength;
            studyFieldEd.maxLen = tbl.fieldOfStudyColumn.MaxLength;
            studyFieldEd.minSelectedItems = 1;
            noteEd.MaxLength = tbl.notesColumn.MaxLength;
        }
        public virtual void Init() 
        {
            countryCb.LoadData();
            degreeCb.LoadData();
            gradYearEd.LoadData();
            studyFieldEd.LoadData();
        }

        public virtual bool CheckData()
        {
            if (this.myDataSource.Current == null) return true;
            bool retVal = true;
            ClearNotifyError();
            if (schoolNameEd.Text.Trim() == "") 
            {
                NotifyError(schoolNameLbl);
                retVal = false;
            }
            if (countryCb.myValue == "") 
            {
                NotifyError(countryLbl);
                retVal = false;
            }
            if (degreeCb.myValue == "")
            {
                NotifyError(degreeLbl);
                retVal = false;
            }
            if (gradYearEd.myYear == 0)
            {
                NotifyError(gradYearLbl);
                retVal = false;
            }
            if (studyFieldEd.minSelectedItems >=0  && studyFieldEd.myCheckedValues.Count < studyFieldEd.minSelectedItems) 
            {
                NotifyError(studyFieldLbl);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            if (myDataSource.Current == null) state = true; 
            schoolNameEd.ReadOnly = state;
            degreeCb.Enabled = !state;
            countryCb.Enabled = !state;
            gradYearEd.Enabled = !state;
            studyFieldEd.Enabled = !state;
            noteEd.ReadOnly = state;
            
        }
        public virtual void SetFocus()
        {
            this.Focus();
            schoolNameEd.Focus();
        }
    }
}
